package com.hsnn.medstgmini.sys.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.sys.dao.SysUserAuditDao;
import com.hsnn.medstgmini.sys.model.AuditType;
import com.hsnn.medstgmini.sys.model.RegCompInfo;
import com.hsnn.medstgmini.sys.model.RegHospInfo;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.PinYin;

/**
 * @category 注册用户审核
 * @author 蔡春龙
 * @date 2015年6月25日
 */
@Repository
public class SysUserAuditDaoImpl implements SysUserAuditDao {

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * @category 根据id查询医疗机构注册信息
	 * @date 2015年6月24日
	 * @param hospId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public RegHospInfo getAuditHospById(String hospId) {
		Session session = sessionFactory.getCurrentSession();
		if (StringUtils.isNotBlank(hospId)) {
			Criteria criteria = session.createCriteria(RegHospInfo.class);
			criteria.add(Restrictions.eq("hospId", hospId));
			List<RegHospInfo> list = (List<RegHospInfo>) criteria.list();
			if (list != null && !list.isEmpty()) {
				return list.get(0);
			}
		}
		return null;
	}

	/**
	 * @category 分页查询医疗机构注册信息审核列表
	 * @date 2015年6月24日
	 * @param page
	 * @param hospInfo
	 * @return
	 */
	public Pagination getAuditUserList(Pagination page, RegHospInfo hospInfo) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(RegHospInfo.class);
		criteria.add(Restrictions.eq("auditStatus", AuditType.normal.getKey()));

		// 搜索条件
		if (hospInfo != null) {
			String hospCode = hospInfo.getHospCode();
			if (StringUtils.isNotBlank(hospCode)) {
				criteria.add(Restrictions.eq("hospCode",hospCode.trim()));
			}
			String hospName = hospInfo.getHospName();
			if (StringUtils.isNotBlank(hospName)) {
				criteria.add(Restrictions.or(Restrictions.like("hospName",
						"%" + hospName.trim() + "%"),Restrictions.eq("hospSpelCode", PinYin.getPinYin(hospName))));
			}
		}

		// 查询个数
		criteria.setProjection(Projections.rowCount());
		Long records = (Long) criteria.uniqueResult();
		page.setRecords(records);

		// 查询列表
		criteria.setProjection(null);
		criteria.addOrder(Order.asc("auditStatus"));
		criteria.addOrder(Order.desc("regDateTime"));
		criteria.setFirstResult(page.getFirstResult());
		criteria.setMaxResults(page.getMaxResults());
		page.setRows(criteria.list());

		return page;
	}

	/**
	 * @category 分页查询企业注册信息审核列表
	 * @date 2015年6月24日
	 * @param page
	 * @param hospInfo
	 * @return
	 */
	public Pagination getAuditUserList(Pagination page, RegCompInfo compInfo) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(RegCompInfo.class);
		criteria.add(Restrictions.eq("compType", compInfo.getCompType()));
		criteria.add(Restrictions.eq("auditStatus", AuditType.normal.getKey()));

		// 搜索条件
		if (compInfo != null) {
			String compCode = compInfo.getCompCode();
			if (StringUtils.isNotBlank(compCode)) {
				criteria.add(Restrictions.like("compCode",
						 compCode.trim()));
			}
			String compName = compInfo.getCompName();
			if (StringUtils.isNotBlank(compName)) {
				criteria.add(Restrictions.or(Restrictions.like("compName",
						"%" + compName.trim() + "%"),Restrictions.eq("compSpelCode", PinYin.getPinYin(compName))));
			}
		}

		// 查询个数
		criteria.setProjection(Projections.rowCount());
		Long records = (Long) criteria.uniqueResult();
		page.setRecords(records);

		// 查询列表
		criteria.setProjection(null);
		criteria.addOrder(Order.asc("auditStatus"));
		criteria.addOrder(Order.desc("regDateTime"));
		criteria.setFirstResult(page.getFirstResult());
		criteria.setMaxResults(page.getMaxResults());
		page.setRows(criteria.list());

		return page;
	}

	/**
	 * @category 分页查询医疗机构注册信息审核日志列表
	 * @date 2015年6月24日
	 * @param page
	 * @param hospInfo
	 * @return
	 */
	public Pagination getAuditUserLogList(Pagination page, RegHospInfo hospInfo) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(RegHospInfo.class);
		criteria.add(Restrictions.or(
				Restrictions.eq("auditStatus", AuditType.unaudited.getKey()),
				Restrictions.eq("auditStatus", AuditType.audited.getKey())));

		// 搜索条件
		if (hospInfo != null) {
			String hospCode = hospInfo.getHospCode();
			if (StringUtils.isNotBlank(hospCode)) {
				criteria.add(Restrictions.eq("hospCode",hospCode.trim()));
			}
			String hospName = hospInfo.getHospName();
			if (StringUtils.isNotBlank(hospName)) {
				criteria.add(Restrictions.or(Restrictions.like("hospName",
						"%" + hospName.trim() + "%"),Restrictions.eq("hospSpelCode", PinYin.getPinYin(hospName))));
			}
		}

		// 查询个数
		criteria.setProjection(Projections.rowCount());
		Long records = (Long) criteria.uniqueResult();
		page.setRecords(records);

		// 查询列表
		criteria.setProjection(null);
		criteria.addOrder(Order.asc("auditStatus"));
		criteria.addOrder(Order.desc("regDateTime"));
		criteria.setFirstResult(page.getFirstResult());
		criteria.setMaxResults(page.getMaxResults());
		page.setRows(criteria.list());

		return page;
	}

	/**
	 * @category 分页查询企业注册信息审核日志列表
	 * @date 2015年6月24日
	 * @param page
	 * @param hospInfo
	 * @return
	 */
	public Pagination getAuditUserLogList(Pagination page, RegCompInfo compInfo) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(RegCompInfo.class);
		criteria.add(Restrictions.eq("compType", compInfo.getCompType()));
		criteria.add(Restrictions.or(
				Restrictions.eq("auditStatus", AuditType.unaudited.getKey()),
				Restrictions.eq("auditStatus", AuditType.audited.getKey())));

		// 搜索条件
		if (compInfo != null) {
			String compCode = compInfo.getCompCode();
			if (StringUtils.isNotBlank(compCode)) {
				criteria.add(Restrictions.eq("compCode",compInfo.getCompCode()));
			}
			String compName = compInfo.getCompName();
			if (StringUtils.isNotBlank(compName)) {
				criteria.add(Restrictions.or(Restrictions.like("compName",
						"%" + compName.trim() + "%"),Restrictions.eq("compSpelCode", PinYin.getPinYin(compName))));
			}
		}

		// 查询个数
		criteria.setProjection(Projections.rowCount());
		Long records = (Long) criteria.uniqueResult();
		page.setRecords(records);

		// 查询列表
		criteria.setProjection(null);
		criteria.addOrder(Order.asc("auditStatus"));
		criteria.addOrder(Order.desc("regDateTime"));
		criteria.setFirstResult(page.getFirstResult());
		criteria.setMaxResults(page.getMaxResults());
		page.setRows(criteria.list());

		return page;
	}

	/**
	 * @category 保存医疗机构注册信息审核
	 * @date 2015年6月23日
	 * @param hospInfo
	 */
	public void saveAuditHosp(RegHospInfo hospInfo) {
		Session session = sessionFactory.getCurrentSession();
		if (hospInfo != null && StringUtils.isNotBlank(hospInfo.getHospId())) {
			RegHospInfo hi = getAuditHospById(hospInfo.getHospId());
			if (hi != null) {
				session.evict(hi);
				hospInfo.setAuditDateTime(new Date());
				hospInfo.setRegDateTime(hi.getRegDateTime());
				session.saveOrUpdate(hospInfo);
				session.flush();
			}
		}
	}

	/**
	 * @category 根据id查询医疗机构注册信息
	 * @date 2015年6月24日
	 * @param compId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public RegCompInfo getAuditCompById(String compId) {
		Session session = sessionFactory.getCurrentSession();
		if (StringUtils.isNotBlank(compId)) {
			Criteria criteria = session.createCriteria(RegCompInfo.class);
			criteria.add(Restrictions.eq("compId", compId));
			List<RegCompInfo> list = (List<RegCompInfo>) criteria.list();
			if (list != null && !list.isEmpty()) {
				return list.get(0);
			}
		}
		return null;
	}

	/**
	 * @category 校验注册的企业在企业库中是否存在
	 * @date 2015年6月24日
	 * @param hospInfo
	 * @return
	 */
	public boolean checkCompExist(RegCompInfo compInfo) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(StdCompinfo.class);
		if (compInfo != null) {
			//criteria.add(Restrictions.eq("compCode", compInfo.getCompCode()));
			criteria.add(Restrictions.eq("compName", compInfo.getCompName()));
			criteria.setProjection(Projections.rowCount());
			Long records = (Long) criteria.uniqueResult();
			if (records != null && records > 0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @category 获取企业库中已存在的信息
	 * @date 2015年6月26日
	 * @param compInfo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public StdCompinfo getExistComp(RegCompInfo compInfo) {
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(StdCompinfo.class);
		if (compInfo != null) {
			//criteria.add(Restrictions.eq("compCode", compInfo.getCompCode()));
			criteria.add(Restrictions.eq("compName", compInfo.getCompName()));
			List<StdCompinfo> list = criteria.list();
			if (list != null && !list.isEmpty()) {
				return list.get(0);
			}
		}
		return null;
	}

	/**
	 * @category 保存企业注册信息审核
	 * @date 2015年6月23日
	 * @param hospInfo
	 */
	public void saveAuditComp(RegCompInfo compInfo) {
		Session session = sessionFactory.getCurrentSession();
		if (compInfo != null && StringUtils.isNotBlank(compInfo.getCompId())) {
			RegCompInfo hi = getAuditCompById(compInfo.getCompId());
			if (hi != null) {
				session.evict(hi);
				compInfo.setAuditDateTime(new Date());
				compInfo.setRegDateTime(hi.getRegDateTime());
				session.saveOrUpdate(compInfo);
				session.flush();
			}
		}
	}

	
	/**
	 * @category 根据医疗机构相应等级获取其Name
	 * @author 韩守松
	 * @date   2015年10月23日
	 * @param  @param classifyId
	 * @param  @return
	 */
	public String getClassifyName(String classifyId) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer hql = new StringBuffer();
		hql.append("select dic_name from dic_sysdic where classify_code = '113' and dic_code = :classifyId ");
		SQLQuery query = session.createSQLQuery(hql.toString());
		query.setString("classifyId", classifyId);
		return query.list().get(0).toString();
	}
	
	/**
	 * @category 根据医疗机构相应等级获取其Name
	 * @author 韩守松
	 * @date   2015年10月23日
	 * @param  @param classifyId
	 * @param  @return
	 */
	public String getLevelName(String levelId) {
		Session session = sessionFactory.getCurrentSession();
		StringBuffer hql = new StringBuffer();
		hql.append("select dic_name from dic_sysdic where classify_code = '002'  and dic_code = :levelId ");
		SQLQuery query = session.createSQLQuery(hql.toString());
		query.setString("levelId", levelId);
		return query.list().get(0).toString();
	}
}
